package com.chenfan.filestore.model;

import com.chenfan.infra.context.UserContextHolder;
import com.chenfan.infra.vo.UserVO;
import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.Data;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.Objects;

@Data
public class BaseEntity {

    public static final int DELETE_YES = 1;
    public static final int DELETE_NO = 0;

    private Integer status;

    private Integer isDelete;

    private Long tenantId;

    private Long companyId;

    private Long createBy;

    private String createName;

    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    private Date createDate;

    private Long updateBy;

    private String updateName;


    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    private Date updateDate;

    public void setUp() {
        UserVO userVo = UserContextHolder.getUserVo();
        if (Objects.nonNull(userVo)) {
            setCreateBy(userVo.getUserId());
            setCreateName(userVo.getRealName());
            setUpdateBy(userVo.getUserId());
            setUpdateName(userVo.getRealName());
            setTenantId(userVo.getTenantId());
            setCompanyId(userVo.getCompanyId());

        }
        setCreateDate(new Date());
        setUpdateDate(new Date());
    }

    public void updateSelf() {
        UserVO userVo = UserContextHolder.getUserVo();
        if (Objects.nonNull(userVo)) {
            setUpdateBy(userVo.getUserId());
            setUpdateName(userVo.getRealName());
        }
        setUpdateDate(new Date());
    }
}
